--[[
任务目标逻辑封装
--]]
local Util = require("common.Util")
local Map = require("core.Map")
local Msg = require("core.Msg")
local ExcelMonsterConf = require("excel.Monster")
local ExcelCollectConf = require("excel.Collect")
local TaskDefine = require("task.Define")
local BagLogic = require("bag.BagLogic")
local TaskLogic = require("task.TaskLogic")

-- 根据任务目标确定任务类型
function getTaskType2(taskConfig)
	return taskConfig.target
end

-- 判断任务目标是否达成
function isTaskActionFinish(human, taskID, taskConfig, record)
  record = record or TaskLogic.getTaskRecord(human, taskID)

	local type2 = getTaskType2(taskConfig)
	
	if type2 == TaskDefine.TASK_TYPE_2_TALK then
		return TaskDefine.TASK_FINISH_OK	-- 对话任务 直接允许完成
	end
	
	if type2 == TaskDefine.TASK_TYPE_2_MONSTER_TARGET 
		or type2 == TaskDefine.TASK_TYPE_2_MONSTER_LEVEL
		or type2 == TaskDefine.TASK_TYPE_2_MONSTER_TITLE
		or type2 == TaskDefine.TASK_TYPE_2_COLLECT 
		or type2 == TaskDefine.TASK_TYPE_2_REFINE 
		or type2 == TaskDefine.TASK_TYPE_2_REFINE_POS
		or type2 == TaskDefine.TASK_TYPE_2_ACT 
		or type2 == TaskDefine.TASK_TYPE_2_FRIEND 
		or type2 == TaskDefine.TASK_TYPE_2_INTERACT
		or type2 == TaskDefine.TASK_TYPE_2_COPY 
		or type2 == TaskDefine.TASK_TYPE_2_MONSTER_COLLECT 
		or type2 == TaskDefine.TASK_TYPE_2_REACH_AREA_MULTI 
		or type2 == TaskDefine.TASK_TYPE_2_COPY_MULTI then
		if record.finishFlag == TaskDefine.TASK_FINISH_FLAG_CAN_FINISH then
			return TaskDefine.TASK_FINISH_OK
		else
			return TaskDefine.TASK_FINISH_FAIL_NOT_ENOUGH_NUM		
		end
	elseif type2 == TaskDefine.TASK_TYPE_2_GET_ITEM then
		-- 这里要判断背包里面有没有足够的道具
		local hasEnoughItem = true
		for itemID, itemCount in pairs(taskConfig.targetParam) do
			if BagLogic.getItemCount(human, itemID) < itemCount then
				hasEnoughItem = false
				break
			end
		end
		if hasEnoughItem == true then 
			return TaskDefine.TASK_FINISH_OK
		else
			return TaskDefine.TASK_FINISH_FAIL_CONDITION
		end
	elseif type2 == TaskDefine.TASK_TYPE_2_REACH_AREA then
		-- 这里要判断角色是否到达了特定的区域
		local map = taskConfig.targetParam[1]
		local x = taskConfig.targetParam[2]
		local y = taskConfig.targetParam[3]
		local radius = taskConfig.targetParam[4] + 200
		
		if human.scene_id == nil or map ~= Map.scene_id_to_map_id[human.scene_id] then
			return TaskDefine.TASK_FINISH_FAIL_AREA 
		end
		if Util.dist(x, y, human.x, human.y) > radius then
			return TaskDefine.TASK_FINISH_FAIL_AREA 
		end
		return TaskDefine.TASK_FINISH_OK
	elseif type2 == TaskDefine.TASK_TYPE_2_USE_ITEM then
		local record = human.db.taskRecord[taskID]
		if record.canFinish == true then
			return TaskDefine.TASK_FINISH_OK
		else
			return TaskDefine.TASK_FINISH_FAIL_CONDITION
		end
	end

	assert(nil)
end


local function getTargetFinishNum(record, targetID)
  for i = 0, record.paramsLen - 1 do
      if record.params[i].target == targetID then
         return record.params[i].num
      end
  end
  return 0
end


-- 根据任务的action 填充taskObj
function setTaskObj(taskNode, human, taskID, taskConfig, taskRecord)
	local taskRecordInfo = taskRecord or TaskLogic.getTaskRecord(human, taskID)
	taskNode.taskObj[0] = 0	
		
	if taskNode.taskType2 == TaskDefine.TASK_TYPE_2_MONSTER_TARGET or 
		taskNode.taskType2 == TaskDefine.TASK_TYPE_2_COLLECT then
		local targetCount = 0
		for targetID, targetNum in pairs(taskConfig.targetParam) do
			targetCount = targetCount + 1
			taskNode.taskObj[targetCount].id = targetID
			if taskRecordInfo then
				taskNode.taskObj[targetCount].cur = getTargetFinishNum(taskRecordInfo, targetID)
			else
				taskNode.taskObj[targetCount].cur = 0
			end
			taskNode.taskObj[targetCount].max = targetNum	
			
			if taskNode.taskType2 == TaskDefine.TASK_TYPE_2_MONSTER_TARGET then
				taskNode.taskObj[targetCount].map = ExcelMonsterConf.monster[targetID].mapID
				taskNode.taskObj[targetCount].x = ExcelMonsterConf.monster[targetID].mapX
				taskNode.taskObj[targetCount].y = ExcelMonsterConf.monster[targetID].mapY
			else
				taskNode.taskObj[targetCount].map = ExcelCollectConf.Collect[targetID].mapID
				taskNode.taskObj[targetCount].x = ExcelCollectConf.Collect[targetID].mapX
				taskNode.taskObj[targetCount].y = ExcelCollectConf.Collect[targetID].mapY
			end
		end
		taskNode.taskObj[0] = targetCount
	elseif taskNode.taskType2 == TaskDefine.TASK_TYPE_2_MONSTER_LEVEL then
		taskNode.taskObj[0] = 1
		taskNode.taskObj[1].id = 0
		if taskRecordInfo then
			taskNode.taskObj[1].cur = getTargetFinishNum(taskRecordInfo, 0)
		else
			taskNode.taskObj[1].cur = 0
		end
		taskNode.taskObj[1].max = taskConfig.targetParam[1]
		taskNode.taskObj[1].map = 0
		taskNode.taskObj[1].x = 0
		taskNode.taskObj[1].y = 0
		if taskConfig.targetParam[1] == nil then
			assert(nil)
		end
	elseif taskNode.taskType2 == TaskDefine.TASK_TYPE_2_MONSTER_TITLE then
		taskNode.taskObj[0] = 1
		taskNode.taskObj[1].id = taskConfig.targetParam[2]
		if taskRecordInfo then
			taskNode.taskObj[1].cur = getTargetFinishNum(taskRecordInfo, 0)
		else
			taskNode.taskObj[1].cur = 0
		end
		taskNode.taskObj[1].max = taskConfig.targetParam[1]
		taskNode.taskObj[1].map = 0
		taskNode.taskObj[1].x = 0
		taskNode.taskObj[1].y = 0
		if taskConfig.targetParam[1] == nil then
			assert(nil)
		end
	elseif taskNode.taskType2 == TaskDefine.TASK_TYPE_2_USE_ITEM then
		taskNode.taskObj[0] = 1
		taskNode.taskObj[1].id = taskConfig.targetParam[1]
		taskNode.taskObj[1].cur = 0
		taskNode.taskObj[1].max = 1
		taskNode.taskObj[1].map = 0
		taskNode.taskObj[1].x = 0
		taskNode.taskObj[1].y = 0
		if taskConfig.targetParam[1] == nil then
			assert(nil)
		end
	elseif taskNode.taskType2 == TaskDefine.TASK_TYPE_2_GET_ITEM then
		local targetCount = 0
		for targetID, targetNum in pairs(taskConfig.targetParam) do
			targetCount = targetCount + 1
			taskNode.taskObj[targetCount].id = targetID
			taskNode.taskObj[targetCount].cur = 0
			taskNode.taskObj[targetCount].max = targetNum
			taskNode.taskObj[targetCount].map = 0
			taskNode.taskObj[targetCount].x = 0
			taskNode.taskObj[targetCount].y = 0
		end
		taskNode.taskObj[0] = targetCount
	elseif taskNode.taskType2 == TaskDefine.TASK_TYPE_2_REACH_AREA then
		taskNode.taskObj[0] = 1
		taskNode.taskObj[1].id = 0
		taskNode.taskObj[1].cur = 0
		taskNode.taskObj[1].max = 1
		taskNode.taskObj[1].map = taskConfig.targetParam[1]
		taskNode.taskObj[1].x = taskConfig.targetParam[2]
		taskNode.taskObj[1].y = taskConfig.targetParam[3]
		if taskConfig.targetParam[1] == nil then 
			assert(nil)
		end
	elseif taskNode.taskType2 == TaskDefine.TASK_TYPE_2_REACH_AREA_MULTI then
		taskNode.taskObj[0] = #taskConfig.targetParam
		for i = 1, #taskConfig.targetParam do
			taskNode.taskObj[i].id = i
			if taskRecordInfo then
				taskNode.taskObj[i].cur = getTargetFinishNum(taskRecordInfo, i)
			else
				taskNode.taskObj[i].cur = 0
			end
			taskNode.taskObj[i].max = 1
			taskNode.taskObj[i].map = taskConfig.targetParam[i][1]
			taskNode.taskObj[i].x = taskConfig.targetParam[i][2]
			taskNode.taskObj[i].y = taskConfig.targetParam[i][3]		
		end
	elseif taskNode.taskType2 == TaskDefine.TASK_TYPE_2_REFINE or 
		 taskNode.taskType2 == TaskDefine.TASK_TYPE_2_ACT or 
		 taskNode.taskType2 == TaskDefine.TASK_TYPE_2_FRIEND or
		 taskNode.taskType2 == TaskDefine.TASK_TYPE_2_INTERACT or
		 taskNode.taskType2 == TaskDefine.TASK_TYPE_2_COPY then
		if taskConfig.targetParam[1] == nil or taskConfig.targetParam[2] == nil then 
			assert(nil)
		end
		taskNode.taskObj[0] = 1
		taskNode.taskObj[1].id = taskConfig.targetParam[1]
		if taskRecordInfo then
			taskNode.taskObj[1].cur = getTargetFinishNum(taskRecordInfo, taskConfig.targetParam[1])
		else
			taskNode.taskObj[1].cur = 0
		end
		taskNode.taskObj[1].max = taskConfig.targetParam[2]
		taskNode.taskObj[1].map = 0
		taskNode.taskObj[1].x = 0
		taskNode.taskObj[1].y = 0
  elseif taskNode.taskType2 == TaskDefine.TASK_TYPE_2_REFINE_POS then
    if taskConfig.targetParam[1] == nil or taskConfig.targetParam[2] == nil or taskConfig.targetParam[3] == nil then 
      assert(nil)
    end
    taskNode.taskObj[0] = 1
    taskNode.taskObj[1].id = taskConfig.targetParam[1]
    if taskRecordInfo then
      taskNode.taskObj[1].cur = getTargetFinishNum(taskRecordInfo, taskConfig.targetParam[1])
    else
      taskNode.taskObj[1].cur = 0
    end
    taskNode.taskObj[1].max = taskConfig.targetParam[2]
    taskNode.taskObj[1].map = 0
    taskNode.taskObj[1].x = taskConfig.targetParam[3]
    taskNode.taskObj[1].y = 0
	elseif taskNode.taskType2 == TaskDefine.TASK_TYPE_2_MONSTER_COLLECT then
		if taskConfig.targetParam.c == nil or taskConfig.targetParam.m == nil then 
			assert(nil, " no c or no m taskID:" .. taskID)
		end
		
		local targetCount = 0
		for targetID, targetNum in pairs(taskConfig.targetParam.c) do
			targetCount = targetCount + 1
			taskNode.taskObj[targetCount].id = targetID
			if taskRecordInfo then
				taskNode.taskObj[targetCount].cur = getTargetFinishNum(taskRecordInfo, targetID)
			else
				taskNode.taskObj[targetCount].cur = 0
			end
			taskNode.taskObj[targetCount].max = targetNum	
			
			taskNode.taskObj[targetCount].map = ExcelCollectConf.Collect[targetID].mapID
			taskNode.taskObj[targetCount].x = ExcelCollectConf.Collect[targetID].mapX
			taskNode.taskObj[targetCount].y = ExcelCollectConf.Collect[targetID].mapY
		end
		for targetID, targetNum in pairs(taskConfig.targetParam.m) do
			targetCount = targetCount + 1
			taskNode.taskObj[targetCount].id = targetID
			if taskRecordInfo then
				taskNode.taskObj[targetCount].cur = getTargetFinishNum(taskRecordInfo, targetID)
			else
				taskNode.taskObj[targetCount].cur = 0
			end
			taskNode.taskObj[targetCount].max = targetNum	
			
			taskNode.taskObj[targetCount].map = ExcelMonsterConf.monster[targetID].mapID
			taskNode.taskObj[targetCount].x = ExcelMonsterConf.monster[targetID].mapX
			taskNode.taskObj[targetCount].y = ExcelMonsterConf.monster[targetID].mapY
		end
		
		taskNode.taskObj[0] = targetCount
	elseif taskNode.taskType2 == TaskDefine.TASK_TYPE_2_COPY_MULTI then
		taskNode.taskObj[0] = 1
		taskNode.taskObj[1].id = taskConfig.targetParam[1]
		if taskRecordInfo then
			taskNode.taskObj[1].cur = getTargetFinishNum(taskRecordInfo, 0)
		else
			taskNode.taskObj[1].cur = 0
		end
		taskNode.taskObj[1].max = 1
		taskNode.taskObj[1].map = 0
		taskNode.taskObj[1].x = 0
		taskNode.taskObj[1].y = 0
	end
end

function playMovie(human, movieID)
	if movieID == nil or movieID[1] == nil then return end
	
	local mm = Msg.gc.GC_SCRIPT_START
	mm.script_id = movieID[1]
	mm.delay = movieID[2]
	
	print("movieID[1]:", movieID[1], " movieID[2]:", movieID[2])
	
	Msg.send(mm, human.fd)
	
	-- 任务里面的剧情为了防止玩家不动 被怪或者人打死 这里加一个无敌的buff
--	Buffer.add(85000, human, human)
end